package com.sinothk.amap.choose.demo

import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.amap.api.location.AMapLocationClient
import com.sinothk.amap.choose.bean.ChoosePoiItemBean
import com.sinothk.amap.choose.LocationHelper
import com.sinothk.amap.choose.SearchMainActivity
import kotlinx.android.synthetic.main.activity_main_demo.*

class MainDemoActivity : AppCompatActivity() {

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main_demo)

        AMapLocationClient.updatePrivacyAgree(this.applicationContext, true);
        AMapLocationClient.updatePrivacyShow(this.applicationContext, true, true);

        doSelectBtn.setOnClickListener {
            val myIntent = Intent(this@MainDemoActivity, SearchMainActivity::class.java)
            myIntent.putExtra("id","123456789")
            startActivityForResult(myIntent, 300)
        }

        LocationHelper.obj().singleLocation(this) { location ->
            if (location != null) {
                val str = addressTv.text.toString()
                addressTv.text = str + "\n" + location.latitude
            }
        }

//        LocationHelper.obj().recycleLocation(this,3000) { amapLocation ->
//            if (amapLocation != null) {
//                val str = addressTv.text.toString()
//                addressTv.text = str + "\n" + amapLocation.address
//            }
//        }

        stopLocateBtn.setOnClickListener {
            LocationHelper.obj().onDestroy()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        LocationHelper.obj().onDestroy();
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != 200 || data == null) {
            return
        }

        when (requestCode) {
            300 -> {
                val choosePoiItem: ChoosePoiItemBean = data.getSerializableExtra("choosePoiItem") as ChoosePoiItemBean
                addressTv.text = choosePoiItem.toString()
            }
        }
    }
}